import com.adobe.cairngorm.control.CairngormEventDispatcher;

import flash.events.Event;

import net.optix.ip.events.GetBudgetsEvent;
import net.optix.ip.events.GetFirmsEvent;
import net.optix.ip.events.RequestBidEvent;
import net.optix.ip.model.ModelLocator;
import net.optix.ip.vo.RequestBidVO;

[Bindable]
private var model:ModelLocator	=	ModelLocator.getInstance();

private function showMoney(event:Event):void
{
	event.target.text	=	purseMoney(getMoney(event.target.text));
}

private function getMoney(money:String):String
{
	var str:String		=	"";
	for(var i:int=0; i<money.length; i++)
	{
		if(money.charAt(i) != "," && money.charAt(i) != " ")
		{
			str	=	str+money.charAt(i);
		}
	}
	return str;
}

private function purseMoney(money:String):String
{
	var arr:Array	=	money.split(".");
	if(arr.length == 1)
	{
		return purseMoneyBefore(money);
	}
	else if(arr.length == 2)
	{
		var str1:String;
		str1	=	purseMoneyBefore(arr[0])+" . "+purseMoneyAfter(arr[1]);
		return str1;
	}
	return "";
}

private function purseMoneyBefore(money:String):String
{
	var str:String	=	money.charAt(0);
	var length:int	=	money.length;
		
	for(var i:int=1; i<money.length; i++)
	{
		if(((length-i)/3).toString().length == 1)
		{
			str	=	str+" , "+money.charAt(i);
		}
		else
		{
				
			str	=	str+money.charAt(i);
		}
	}
	return str;
}

private function purseMoneyAfter(money:String):String
{
	var str:String	=	money.charAt(0);
	var length:int	=	money.length;
		
	for(var i:int=1; i<money.length; i++)
	{
		if((i/3).toString().length == 1)
		{
			str	=	str+" , "+money.charAt(i);
		}
		else
		{
				
			str	=	str+money.charAt(i);
		}
	}
	return str;
}

private function requestBid(event:Event):void
{
	var result:Boolean	=	true;
	var message:String	=	"";
	var maxBudgetI:int	=	int(getMoney(maxBudget.text));
	var targetDateI:String	=	targetDate.text;
	
	message	=	"";
	
	if(maxBudgetI == 0)
	{
		message	=	"Maxmum budget is required . ";
		result	=	false;
	}
	
	if(targetDateI == "")
	{
		message	+=	"Filling due date is required . ";
		result	=	false;
	}
	
	if(result == true)
	{
		model.bSubmit	=	false;
		resultRequest.text	=	"";
		var vo:RequestBidVO	=	new RequestBidVO();
		
		vo.id				=	model.currentTradeMark.tradeMarkID;
		vo.bBid				=	1;
		vo.maxBudget		=	maxBudgetI;
		vo.fillingDate		=	targetDate.text;
		vo.category			=	1;
		
		
		var requestBidEvent:RequestBidEvent	=	new RequestBidEvent(vo);
		CairngormEventDispatcher.getInstance().dispatchEvent(requestBidEvent);
	}
	else
	{
		resultRequest.text	=	message;
	}
}